<template>
  <div style="padding: 20px;">
    <div class="searchDiv" style="padding-bottom: 20px;">
      <el-form ref="form" :model="query" label-width="70px">
        <el-row :gutter="20">
          <el-col :span="24">
            <el-input v-model="query.goodsId" size="mini" placeholder="请输入商品ID" style="width: 160px;" clearable />
            <el-input
              v-model="query.goodsSn"
              size="mini"
              placeholder="请输入商品编号"
              style="width: 160px; margin-left: -5px;"
              clearable
            />
            <el-input
              v-model="query.name"
              size="mini"
              placeholder="请输入商品名称"
              style="width: 160px; margin-left: -5px;"
              clearable
            />
            <el-button type="primary" icon="el-icon-search" style="margin-left: -5px;" @click="seek">查找</el-button>
            <el-button type="primary" icon="el-icon-edit" style="margin-left: 6px;" @click="add">添加</el-button>
            <el-button type="primary" icon="el-icon-download" style="margin-left: 6px;">导出</el-button>
            <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              :disabled="delId.length == 0"
              style="margin-left: 6px;"
              @click="batchDeletion"
            >批量删除</el-button>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="tables">
      <el-table :data="tableData" style="width: 100%" border @selection-change="SelectionChange">
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-form label-position="left" inline class="demo-table-expand" style="padding-left: 60px;">
              <el-row :gutter="20">
                <el-form-item label="商品编号" style="width: 100%; text-align: left !important; margin-bottom: 0 !important;">
                  <span>{{ props.row.goodsSn }}</span>
                </el-form-item>
              </el-row>
              <el-row :gutter="20">
                <el-form-item label="宣传画廊" style="width: 100%; text-align: left !important; margin-bottom: 0 !important;">
                  <el-image style=" height: 40px; width: 40px; margin-right: 10px;" v-for="(item, index) in props.row.gallery " :src="item" :preview-src-list="props.row.gallery"></el-image>
                </el-form-item>
              </el-row>
              <el-row :gutter="20">
                <el-form-item label="商品介绍" style="width: 100%; text-align: left !important; margin-bottom: 0 !important;">
                  <span>{{ props.row.brief }}</span>
                </el-form-item>
              </el-row>
              <el-row :gutter="20">
                <el-form-item label="商品单位" style="width: 100%; text-align: left !important; margin-bottom: 0 !important;">
                  <span>{{ props.row.unit }}</span>
                </el-form-item>
              </el-row>
              <el-row :gutter="20">
                <el-form-item label="关键字" style="width: 100%; text-align: left !important; margin-bottom: 0 !important;">
                  <span>{{ props.row.keywords }}</span>
                </el-form-item>
              </el-row>
              <el-row :gutter="20">
                <el-form-item label="类目ID" style="width: 100%; text-align: left !important; margin-bottom: 0 !important;">
                  <span>{{ props.row.categoryId }}</span>
                </el-form-item>
              </el-row>
              <el-row :gutter="20">
                <el-form-item label="品牌商ID" style="width: 100%; text-align: left !important; margin-bottom: 0 !important;">
                  <span>{{ props.row.brandId }}</span>
                </el-form-item>
              </el-row>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column type="selection" width="55" />
        <el-table-column prop="id" label="商品ID" width="119" />
        <el-table-column prop="name" label="名称" width="138" />
        <el-table-column prop="picUrl" label="图片" width="110">
          <template v-slot="scope">
            <el-image style=" height: 40px; width: 40px;" :src="scope.row.picUrl" :preview-src-list="[scope.row.picUrl]"></el-image>
          </template>
        </el-table-column>
        <el-table-column prop="shareUrl" label="分享图" width="110" />
        <el-table-column prop="detail" label="详情" width="110">
          <template v-slot="scope">
            <el-button type="primary" @click="view(scope.row)">查看</el-button>
          </template>
        </el-table-column>
        <el-table-column prop="counterPrice" label="市场售价" width="110" />
        <el-table-column prop="retailPrice" label="当前价格" width="110" />
        <el-table-column prop="isNew" label="是否新品" width="110">
          <template v-slot="scope">
            <el-tag v-if="scope.row.isNew == true" type="success">新品</el-tag>
            <el-tag v-if="scope.row.isNew == false">非新品</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="isHot" label="是否热品" width="110">
          <template v-slot="scope">
            <el-tag v-if="scope.row.isHot == true" type="success">热品</el-tag>
            <el-tag v-if="scope.row.isHot == false">非热品</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="isOnSale" label="是否在售" width="110">
          <template v-slot="scope">
            <el-tag v-if="scope.row.isOnSale == true" type="success">在售</el-tag>
            <el-tag v-if="scope.row.isOnSale == false">未售</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template v-slot="scope">
            <el-button size="mini" type="primary" @click="redact(scope.row.id)">编辑</el-button>
            <el-button size="mini" type="danger" @click="expurgate(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="paging">
      <pagination :total="total" :page="query.page" style="margin-top: 20px;" @size="setSize" @current="setCurrent" />
    </div>
    <div class="dialogDiv">
      <el-dialog title="商品详情" :visible.sync="dialogFormVisible">
        <el-form ref="ruleForm" :model="ruleForm">
          <div>
            <img v-for="(item, index) in ruleForm.extractedImages" :key="index" :src="item" alt="" style="width: 100%;">
            <div v-if="!ruleForm.extractedImages.length" style="margin-top: -11px; padding-bottom: 4px;" v-html="ruleForm.extractedText" />
          </div>
        </el-form>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { forEach } from 'lodash'
import { listGoods, deleteGoods } from '../../api/goods'
import pagination from '../promotion/pagination.vue'
export default {
  components: {
    pagination
  },
  data() {
    return {
      query: {
        page: 1,
        limit: 20,
        goodsSn: null,
        name: null,
        goodsId: null
      },
      delId: [],
      tableData: [],
      total: null,
      dialogFormVisible: false,
      ruleForm: {
        extractedImages: [],
        extractedText: null
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 列表
    getList() {
      listGoods(this.query).then(response => {
        if (response.data.errno == 0) {
          this.tableData = response.data.data.list
          this.total = response.data.data.total
        }
      })
    },
    // 查找
    seek() {
      this.getList()
    },
    // 分页
    setSize(val) {
      this.query.page = 1
      this.query.limit = val
      this.getList()
    },
    setCurrent(val) {
      this.query.page = val
      this.getList()
    },
    // 删除 deleteGoods
    expurgate(row) {
      console.log(row)
      this.$confirm('确定删除?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        deleteGoods(row).then(res => {
          if (res.data.errno == 0) {
            this.$notify({
              title: '成功',
              message: '删除成功',
              type: 'success'
            })
            this.getList()
          }
        })
      })
    },
    // 多选
    SelectionChange(val) {
      this.delId = val
    },
    // 批量删除
    batchDeletion() {
      forEach(this.delId, (item) => {
        deleteGoods(item).then(res => {
          if (res.data.errno == 0) {
            this.$notify({
              title: '成功',
              message: '删除成功',
              type: 'success'
            })
            this.getList()
          }
        })
      })
    },
    // 查看详情
    view(row) {
      this.dialogFormVisible = true
      if (row && row.detail) {
        // 提取所有 <img> 标签的 src 属性
        const imgTags = row.detail.match(/<img[^>]+src="([^">]+)"/g)
        const extractedImages = imgTags ? imgTags.map(tag => tag.match(/src="(.+?)"/)[1]) : []
        this.ruleForm.extractedImages = extractedImages

        // 提取纯文本内容（去除所有的 <img> 标签）
        if (extractedImages.length === 0) {
          const extractedText = row.detail.replace(/<img[^>]*>/g, '')
          this.ruleForm.extractedText = extractedText
        }
      } else {
        this.ruleForm.extractedImages = []
        this.ruleForm.extractedText = ''
      }
    },
    // 编辑
    redact(id) {
      this.$router.push({
        path: '/goods/edit',
        query: {
          id
        }
      })
    },
    add() {
      this.$router.push({
        path: '/goods/create',
        query: {}
      })
    }
  }
}
</script>

<style scoped lang="scss">
  ::v-deep tr div {
    text-align: center !important;
  }
  ::v-deep .el-form-item--mini .el-form-item__label {
    width: 100px !important;
    color: #99a9bf !important;
    margin-left: 10px
  }
  .paging {
    margin-top: 10px;
    background: #fff;
    padding: 32px 16px;
  }
  ::v-deep .el-pagination {
    text-align: left !important;
  }
  ::v-deep .dialogDiv .el-dialog {
    text-align: center !important;
    box-shadow: 0 1px 3px rgba(0, 0, 0, .3) !important;
  }
  ::v-deep .el-dialog__body {
    padding: 46px 20px 30px !important;
  }
</style>
